package com.weshare.nd.netty.client;

import com.weshare.nd.cmd.ChatCommand;
import com.weshare.nd.model.ChatMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.log4j.Logger;

public class ChatClientHandler extends SimpleChannelInboundHandler<Object> {

    public static final Logger logger = Logger.getLogger(ChatClientHandler.class);

    @Override
    protected void messageReceived(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        ChatMessage chatMsg = (ChatMessage) o;

        if (chatMsg != null) {
            ChatMessage repMsg = new ChatMessage();

            String command = chatMsg.getCommand();
            String clientName = chatMsg.getClientName();
            String msg = chatMsg.getMsg();

            ChatClient.lastCommand = command;

            switch (command) {
                case ChatCommand.ASK:
                    System.out.println("[@SERVER] : " + msg);
                    break;
                case ChatCommand.NO_REPLY:
                    break;
                case ChatCommand.BROADCAST:
                    System.out.println(msg);
                    break;
                case ChatCommand.PERSON_MAIL:
                    System.out.println("[@SERVER] : " + msg);
                    break;
                default:
                    System.out.println("default");
                    break;
            }
        }
    }

}